package com.RLFZ.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.RLFZ.entity.Hesuan;
import com.RLFZ.service.HesuanService;
import com.RLFZ.vo.DataView;
import com.RLFZ.vo.HeSuanVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 服务控制器
 *核酸检测页面
 * @author zh
 * @since 2022-08-25 17:16:58
 * @description 
 */
@Slf4j
@RequiredArgsConstructor
@Controller
@RequestMapping("/hesuan")
public class HesuanController  {
    @Autowired
    private HesuanService hesuanService;

    @RequestMapping("/toHeSuan")
    public String toHeSuan(){
        return "hesuan/hesuan";
    }
    /**
     * 查询所有角色带有分页，带有条件查询
     */
    @RequestMapping("/loadAllHeSuan")
    @ResponseBody
    public DataView loadAllHeSuan(HeSuanVo hesuanVo){
        QueryWrapper<Hesuan> queryWrapper=new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNoneBlank(hesuanVo.getName()),"name",hesuanVo.getName());
        IPage<Hesuan> ipage=new Page<>(hesuanVo.getPage(),hesuanVo.getLimit());
        hesuanService.page(ipage,queryWrapper);
        return new DataView(ipage.getTotal(),ipage.getRecords());
    }
//    QueryWrapper<XueYuan> queryWrapper=new QueryWrapper<>();
//        queryWrapper.like(StringUtils.isNoneBlank(xueYuanVo.getName()),"name",xueYuanVo.getName());
//    IPage<XueYuan> ipage=new Page<>(xueYuanVo.getPage(),xueYuanVo.getLimit());
//        xueYuanService.page(ipage,queryWrapper);

    @RequestMapping("/addHeSuan")
    @ResponseBody
    public DataView addOrUpdatehesuan(Hesuan Hesuan){
        DataView dataView=new DataView();
        Integer condition = condition(Hesuan);
        // 年龄
        if (condition==101){
            dataView.setMsg("添加失败，年龄格式不符合规格");
            dataView.setCode(100);
            return dataView;
        }
        // 身份证
        if (condition==102){
            dataView.setMsg("添加失败，身份证格式不符合规格");
            dataView.setCode(100);
            return dataView;
        }
        // 手机号
        if (condition==103){
            dataView.setMsg("添加失败，手机号格式不符合规格");
            dataView.setCode(100);
            return dataView;
        }
        // 姓名
        if (condition==104){
            dataView.setMsg("添加失败，姓名格式不符合规格");
            dataView.setCode(100);
            return dataView;
        }
        boolean save = hesuanService.save(Hesuan);
        dataView.setMsg("添加成功");
        dataView.setCode(200);
        return dataView;
    }
    @RequestMapping("/updateHeSuan")
    @ResponseBody
    public DataView updateHeSuan(Hesuan hesuan){
        DataView dataView=new DataView();
//        System.out.println(hesuan);
        Integer condition = condition(hesuan);
        // 年龄
        if (condition==101){
            dataView.setMsg("修改失败，年龄格式不符合规格");
            dataView.setCode(100);
            return dataView;
        }
        // 身份证
        if (condition==102){
            dataView.setMsg("修改失败，身份证格式不符合规格");
            dataView.setCode(100);
            return dataView;
        }
        // 手机号
        if (condition==103){
            dataView.setMsg("修改失败，手机号格式不符合规格");
            dataView.setCode(100);
            return dataView;
        }
        // 姓名
        if (condition==104){
            dataView.setMsg("修改失败，姓名格式不符合规格");
            dataView.setCode(100);
            return dataView;
        }
        boolean save = hesuanService.updateById(hesuan);
        if (save){
            dataView.setMsg("修改成功");
            dataView.setCode(200);
        }
        else {
            dataView.setMsg("修改失败");
            dataView.setCode(100);
        }
        return dataView;
    }
    @RequestMapping("/deleteHeSuan")
    @ResponseBody
    public DataView deleteHeSuan(Integer id){
        DataView dataView=new DataView();
        boolean save = hesuanService.removeById(id);
        dataView.setMsg("删除成功");
        dataView.setCode(200);
        return dataView;
    }

    /**
     * 核酸检测输入条件判断方法
     * @param hesuan
     * @return
     */
    public Integer condition(Hesuan hesuan){
        Integer age = hesuan.getAge();
        String card = hesuan.getCard();
        String phone = hesuan.getPhone();
        String name = hesuan.getName();
        if (age==null||age<0||age>100){
            return 101;
        }
        if (card==null||card.equals("")||card.length()!=18){
            return 102;
        }
        if (phone==null||phone.equals("")||phone.length()!=11){
            return 103;
        }
        if (name==null||name.equals("")||name.length()<1||name.length()>20){
            return 104;
        }
        return 200;
    }

}